import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import java.util.Random;

public class homework_05_circle extends Application {

    private static final int CENTER_X = 300;
    private static final int CENTER_Y = 300;
    private static final int NUM_CIRCLES = 9;
    private static final int BASE_RADIUS = 30;
    private static final int RADIUS_INCREMENT = 20;
    private static final Random random = new Random();
    private static final Color[] PLANET_COLORS = {
            Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.ORANGE,
            Color.PURPLE, Color.CYAN, Color.MAGENTA, Color.PINK
    };

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();

        drawConcentricCirclesAndPlanets(root);

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setTitle("Concentric Circles and Planets");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void drawConcentricCirclesAndPlanets(Pane root) {
        for (int i = 0; i < NUM_CIRCLES; i++) {
            int currentRadius = BASE_RADIUS + i * RADIUS_INCREMENT;
            // 绘制同心圆，使用中点画圆法
            drawCircleByMidPoint(root, CENTER_X, CENTER_Y, currentRadius, Color.BLACK);

            // 模拟行星小圆
            int planetRadius = (i + 1) * 3;
            // 随机生成一个 0 到 360 度之间的角度
            double angle = random.nextInt(360);
            double planetX = CENTER_X + currentRadius * Math.cos(Math.toRadians(angle));
            double planetY = CENTER_Y + currentRadius * Math.sin(Math.toRadians(angle));
            // 绘制实心行星小圆，使用中点画圆法
            drawFilledCircleByMidPoint(root, (int) planetX, (int) planetY, planetRadius, PLANET_COLORS[i % PLANET_COLORS.length]);
        }
    }

    // 中点画圆法函数
    private void drawCircleByMidPoint(Pane root, int centerX, int centerY, int radius, Color color) {
        int x = 0;
        int y = radius;
        int d = 1 - radius;
        plotPoints(root, x, y, centerX, centerY, color);

        while (x < y) {
            if (d < 0) {
                d = d + 2 * x + 3;
            } else {
                d = d + 2 * (x - y) + 5;
                y--;
            }
            x++;
            plotPoints(root, x, y, centerX, centerY, color);
        }
    }

    // 绘制实心圆的中点画圆法函数
    private void drawFilledCircleByMidPoint(Pane root, int centerX, int centerY, int radius, Color color) {
        int x = 0;
        int y = radius;
        int d = 1 - radius;
        plotFilledPoints(root, x, y, centerX, centerY, color);

        while (x < y) {
            if (d < 0) {
                d = d + 2 * x + 3;
            } else {
                d = d + 2 * (x - y) + 5;
                y--;
            }
            x++;
            plotFilledPoints(root, x, y, centerX, centerY, color);
        }
    }

    // 绘制对称点的函数
    private void plotPoints(Pane root, int x, int y, int centerX, int centerY, Color color) {
        drawPixel(root, centerX + x, centerY + y, color);
        drawPixel(root, centerX + y, centerY + x, color);
        drawPixel(root, centerX - x, centerY + y, color);
        drawPixel(root, centerX - y, centerY + x, color);
        drawPixel(root, centerX + x, centerY - y, color);
        drawPixel(root, centerX + y, centerY - x, color);
        drawPixel(root, centerX - x, centerY - y, color);
        drawPixel(root, centerX - y, centerY - x, color);
    }

    // 绘制实心圆对称点并填充
    private void plotFilledPoints(Pane root, int x, int y, int centerX, int centerY, Color color) {
        for (int i = centerX - x; i <= centerX + x; i++) {
            drawPixel(root, i, centerY + y, color);
            drawPixel(root, i, centerY - y, color);
        }
        for (int i = centerX - y; i <= centerX + y; i++) {
            drawPixel(root, i, centerY + x, color);
            drawPixel(root, i, centerY - x, color);
        }
    }

    // 绘制单个像素点，这里用短线段模拟
    private void drawPixel(Pane root, int x, int y, Color color) {
        Line pixel = new Line(x, y, x + 1, y);
        pixel.setStroke(color);
        root.getChildren().add(pixel);
    }

    public static void main(String[] args) {
        launch(args);
    }
}    